#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @Time: 2019/7/22  14:00
# @Author: 余浪人
# @email: yulangren520@gmail.com
import json
from flask import request
from flask_login import current_user
from wtforms import Form, StringField, validators, PasswordField
from apps.lib.public import redis_
from apps.models.member_model import TL_Member_Model


class TL_Member_Form(Form):
    username = StringField(
        '登录名', validators=[validators.DataRequired('请填写登录名!'), validators.Length(2, message='登录名长度不足!')],
        render_kw={"lay-verify": "nikename", "required": "", "class": "layui-input", "style": "width: 60%;"})
    password = PasswordField(
        label=u'密码', validators=[validators.Length(min=6, message='密码至少6位!')],
        render_kw={"required": "", "class": "layui-input", "style": "width: 60%;"})
    password2 = PasswordField(
        label=u'确认密码', validators=[validators.EqualTo('password', '两次密码不一致!')],
        render_kw={"required": "", "class": "layui-input", "style": "width: 60%;"})
    remark = StringField('备注信息', validators=[validators.Optional(True), validators.Length(max=512, message='备注信息过长!')], render_kw={"class": "layui-input", "style": "width: 60%;"})

    def validate_username(self, username):
        cou = TL_Member_Model.query.filter(TL_Member_Model.username == username.data)
        if cou.count() and cou.first().id != current_user.id:
            raise validators.ValidationError("此登录名已存在!")


class TL_Member_Edit_Form(Form):
    username = StringField(
        '登录名', validators=[validators.DataRequired('请填写登录名!'), validators.Length(2, message='登录名长度不足!')],
        render_kw={"required": "", "class": "layui-input layui-disabled", "style": "width: 60%;", "readonly": "readonly"})
    password = PasswordField(
        label=u'密码', validators=[validators.Length(min=6, message='密码至少6位!')],
        render_kw={"required": "", "class": "layui-input", "style": "width: 60%;"})
    password2 = PasswordField(
        label=u'确认密码', validators=[validators.EqualTo('password', '两次密码不一致!')],
        render_kw={"required": "", "class": "layui-input", "style": "width: 60%;"})

    def validate_username(self, username):
        cou = TL_Member_Model.query.filter(TL_Member_Model.username == username.data)
        if cou.count() and cou.first().id != current_user.id:
            raise validators.ValidationError("此登录名已存在!")


class TL_Login_Form(Form):
    username = StringField('登录名', validators=[validators.DataRequired('请输入登录账号!')])
    password = StringField('密码', validators=[validators.DataRequired('请输入账号密码!')])
    verifyCode = StringField('验证码', validators=[validators.DataRequired('请输入验证码!')])

    def validate_username(self, username):
        cou = TL_Member_Model.query.filter(TL_Member_Model.username == username.data).count()
        if cou == 0:
            raise validators.ValidationError("输入的账号不存在!")

    def validate_verifyCode(self, verifyCode):
        r_0 = redis_(db=0)
        if len(verifyCode.data) != 5:
            raise validators.ValidationError("请输入正确的验证码!")
        data = r_0.get(f'tl_{request.remote_addr}')
        if not data:
            raise validators.ValidationError("输入的验证码已过期!")
        if json.loads(data).lower() != (verifyCode.data).lower():
            raise validators.ValidationError("请输入正确的验证码!".lower())
